package com.vn.gofriends.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.vn.gofriends.R;
import com.vn.gofriends.adapter.PlaceAdapter;
import com.vn.gofriends.item.City;
import com.vn.gofriends.item.PlaceItem;

public class PlaceActivity extends Activity {
	Button btnFinish;
	List<ParseObject> ob;
	ListView lvPlace;
	ArrayList<PlaceItem> arrPlace;
	PlaceAdapter adapter;
	Intent intent;
	String nameCity;
	City city;

	ProgressDialog mProgressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_place);
		btnFinish = (Button) findViewById(R.id.btnFinish);
		btnFinish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(PlaceActivity.this, CreateMyTripActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});
		intent = getIntent();
		Bundle b = intent.getBundleExtra("extraDestination");
		city = (City) b.getSerializable("destination");
		new RemoteDataTask().execute();
	}

	private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgressDialog = new ProgressDialog(PlaceActivity.this);
			mProgressDialog.setTitle("Load place");
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Place");
			query.orderByDescending("createdAt");
			try {
				ob = query.find();
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			lvPlace = (ListView) findViewById(R.id.lvPlace);
			arrPlace = new ArrayList<PlaceItem>();
			
			for (ParseObject place : ob) {
				String idCity = place.getParseObject("city").getObjectId();
				if (idCity.equals(city.getIdCity())) {
					PlaceItem p = new PlaceItem();
					p.setAddress(place.getString("address"));
					p.setPlaceName(place.getString("placeName"));
					arrPlace.add(p);
					Log.d("Ngan", "name: "+p.getPlaceName().toString());
				}
			}
			adapter = new PlaceAdapter(PlaceActivity.this, R.layout.item_place,
					arrPlace);
			lvPlace.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			super.onPostExecute(result);
			mProgressDialog.dismiss();
		}
	}
}
